package model;

import com.trolltech.qt.core.QPoint;

public class CarLocation {
	
	private Street street;
	private int  position;
	public CarLocation(Street street, Integer index) {
		this.street=street;
		this.position=index;
	}
	public Street getStreet() {
		return street;
	}
	public int getPosition() {
		return position;
	}
	public void setStreet(Street street) {
		this.street = street;
	}
	public void setPosition(int position) {
		this.position = position;
	}
	@Override
	public String toString() {
		return "CarLocation [street=" + street + ", position=" + position + "]";
	}
	
	
	
    public static Car carFromMatrix(int x,int y ,int dx,int dy){    	
//    	System.out.println("( "+x+","+y+")");
    	
    	CarLocation start = carLocationFromMatrix(x,y);
    	CarLocation end = carLocationFromMatrix(dx,dy);
    	
    	
//    	System.out.println(start);
    	
    	Car out = new Car(start.getStreet(),
    			start.getPosition(),
    			end.getStreet(),
    			end.getPosition()
    			);
    	
		return out;
    }
	
	
	

	public static CarLocation carLocationFromMatrix(final int x,final  int y) {
    	Integer index = 0;

    	Crossroads begin = null;
    	Crossroads end = null;
    	
    	int bs = Config.nCellsPalace; 
    	int rs = Config.nCellsRoad;
    	//building  + road
    	int brSize = bs + rs;
        	
    	int xXRoad = x/brSize;
    	int yXRoad = y/brSize;
    	
    	if(x>=2 &&(xXRoad == (x-rs)/brSize ) ){ //horizontal road
    		index = x - (xXRoad*brSize +2);
    		if(y%brSize == 0){                 //<-
    			index = bs -1 -index;
//    			System.out.println("  <- " +  index);
    			begin = new Crossroads(xXRoad+1, yXRoad);
    			end = new Crossroads(xXRoad, yXRoad);
    		}else{							   //->
//    			System.out.println("  -> " +  index);
    			begin = new Crossroads(xXRoad, yXRoad);
    			end = new Crossroads(xXRoad+1, yXRoad);    			
    			
    		}
    	}
    	else if(y>=2&&(yXRoad == (y-rs)/brSize ) ){//vertical road 
    		index = y - (yXRoad*brSize +2);
    		if(x%brSize == 0){                 //  V
//    			System.out.println("  V  " +  index);
    			begin = new Crossroads(xXRoad,yXRoad);
    			end = new Crossroads(xXRoad,yXRoad+1);
    		}else{							   //  ^
    			index = bs -1 -index;
//    			System.out.println("  ^  " +  index);
    			begin = new Crossroads(xXRoad,yXRoad+1);
    			end = new Crossroads( xXRoad,yXRoad);
    		}
    	}
    			
		return new CarLocation(new Street(begin,end),index);
	}
	
	  /**
     * Non gestisce valori non validi 
     */

	public static QPoint matrixFromCarLocation(CarLocation c) {
		final int buildSize = Config.nCellsPalace;
		final int road = Config.nCellsRoad;
		
		final Street street = c.getStreet();
		
		int lastX=0;
		int lastY=0;
		
		int direction = 0; // go left o down
		
		if(street.getStart().getY()==(street.getEnd().getY())){ 		// H street
			final int min = Math.min(street.getStart().getX(), street.getEnd().getX());
			
			
			if(street.getStart().getX()<street.getEnd().getX()){  	//    	dir =  ->
				direction = 1;
				lastX = min * buildSize +road * (min+1) + c.getPosition();
			}else{	 												//		dir =  <-
				lastX = min * buildSize +road * (min+1) + ((Config.nCellsPalace-1) - c.getPosition());
			}
			
			lastY = street.getStart().getY()* buildSize +road * (street.getStart().getY()) + direction;
		}else{ 														// V Street
			
			
			final int min = Math.min(street.getStart().getY(), street.getEnd().getY());
			lastY = min * buildSize +road * (min+1) + c.getPosition();
			
			if(street.getStart().getY()<street.getEnd().getY()){ 		 //    dir =  V
				lastY = min * buildSize +road * (min+1) + c.getPosition();
			}
			else{//	   dir =  ^
				lastY = min * buildSize +road * (min+1) + ((Config.nCellsPalace-1) - c.getPosition());
				direction=1;
			}
			lastX = street.getStart().getX()* buildSize +road * (street.getStart().getX()) + direction;
		}
		
		return new QPoint(lastX,lastY);
	}
	
	

}
